﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;
using Iservices;
using System.Text;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace UI.Controllers
{
  
    public class salaryCriterionController : Controller
    {
        private readonly IuserServices Ius;
        private readonly Isalary_standardDao Iss;
        private readonly Iconfig_public_charServices Icp;
        private readonly lsalary_standard_details Issd;
        public salaryCriterionController(Isalary_standardDao Iss, Iconfig_public_charServices Icp, lsalary_standard_details Issd, IuserServices Ius)
        {
            this.Iss = Iss;
            this.Icp = Icp;
            this.Issd = Issd;
            this.Ius = Ius;
        }
        //薪酬标准登记
        public async Task<ActionResult> salarystandard_register() {
            salary_standard sa = new salary_standard();
            var isa = await Iss.Select();
            if (isa.Count > 0)
            {
                var _Standard = isa[isa.Count - 1];
                int intcod = Convert.ToInt32(_Standard.standard_id.Substring(2, 10));
                intcod++;
                sa.standard_id = intcod.ToString();
                for (int i = 0; i < 10; i++)
                {
                    sa.standard_id = sa.standard_id.Length < 10 ? "0" + sa.standard_id : sa.standard_id;
                }
                sa.standard_id = "OR" + sa.standard_id;
            }
            else
            {
                sa.standard_id = "OR0000000001";
            }
            sa.register = "admin";          
            ViewData.Model = sa;

            List<user> dt = await Ius.Select();
            List<SelectListItem> list = new List<SelectListItem>();
            foreach (user dr2 in dt)
            {
                SelectListItem sl = new SelectListItem()
                {
                    Text = dr2.user_true_name.ToString(),
                    Value = dr2.user_true_name.ToString()
                };
                list.Add(sl);
            }
            ViewBag.s = list;
            return View();


        }
        public async Task<ActionResult> Index2()
        {
            List<config_public_char> list = await Icp.TestSelect();
            List<config_public_char> t = list.Where(e => e.attribute_kind == "薪酬设置")
                     .ToList();
            string zhi = JsonConvert.SerializeObject(t);
            return Content(zhi);
        }
        public async Task<IActionResult> Add(salary_standard ss, string[] salary, string[] item_id, string[] item_name)
        {
            if (ModelState.IsValid)
            {
                salary_standard_details ssd = new salary_standard_details()
                {
                    standard_id = ss.standard_id,
                    standard_name = ss.standard_name                  
                };
               
                int result2 = 0;
                for (int i = 0; i < salary.Length; i++)
                {
                    ssd.item_id = (short)Convert.ToInt32(item_id[i]);
                    ssd.item_name = item_name[i];
                    ssd.salary = Convert.ToDecimal(salary[i]);
                    result2 = await Issd.Salaryadd(ssd);
                }
                int result = await Iss.Add(ss);
                if (result2 > 0)
                {
                    return Content("<script>alert('新增成功');window.location.href='/salaryCriterion/salarystandard_check_list'</script>", "text/html", Encoding.Default);
                }
                else
                {
                    return Content("<script>alert('新增失败');window.location.href='/salaryCriterion/salarystandard_register'</script>", "text/html", Encoding.Default);
                }
            }
            return Content("<script>alert('非空选项不能为空');window.location.href='/salaryCriterion/salarystandard_register'</script>", "text/html", Encoding.Default);
        }



        //薪酬标准登记复核
        public  ActionResult salarystandard_check_list() {                      
            return View();

        }
        public async Task<ActionResult> Index(int currentPage)
        {
            FenYe fy = new FenYe()
            {
                Where = "check_status =1",
                CurrentPage = Convert.ToInt32(currentPage),
                PageSize = 3
            };
            Dictionary<string, object> list = await Iss.FenYeSelectSelect(fy);
            string zhi = JsonConvert.SerializeObject(list);
            return Content(zhi);
        }

        //薪酬标准查询
        public ActionResult salarystandard_query_locate() {

            return View();
        }
        public IActionResult salarystandard_query_list(cx ss)
        {
            string s = ss.name + "," + ss.guan + "," + ss.qi + "," + ss.si;
            ViewBag.s = s;
            return View();
        }


       


    

    //薪酬标准登记复核通过
    [HttpGet]
        public async Task<ActionResult> salarystandard_check(int id) {
            List<user> dt = await Ius.Select();
            List<SelectListItem> list = new List<SelectListItem>();
            foreach (user dr2 in dt)
            {
                SelectListItem sl = new SelectListItem()
                {
                    Text = dr2.user_true_name.ToString(),
                    Value = dr2.user_true_name.ToString()
                };
                list.Add(sl);
            }
            ViewBag.s = list;
            List<salary_standard> dt1 = await Iss.Select();
            salary_standard t = dt1.Where(e => e.Id == id)
                 .FirstOrDefault();
            t.checker = "admin";
            ViewData.Model = t;
            return View();



           
        }
        public async Task<ActionResult> Index3(string id)
        {
            List<salary_standard_details> list = await Issd.Select();
            List<salary_standard_details> t = list.Where(e => e.standard_id == id)
                     .ToList();
            string zhi = JsonConvert.SerializeObject(t);
            return Content(zhi);
        }

        [HttpPost]
        public async Task<ActionResult> salarystandard_check(salary_standard ss, string[] salary, string[] item_id, string[] item_name, string[] Id)
        {


            ss.check_status = 2;
            salary_standard_details ssd = new salary_standard_details()
            {
                standard_id = ss.standard_id,
                standard_name = ss.standard_name
            };
            int result2 = 0;
            for (int i = 0; i < salary.Length; i++)
            {
                ssd.Id = (short)Convert.ToInt32(Id[i]);
                ssd.item_id = (short)Convert.ToInt32(item_id[i]);
                ssd.item_name = item_name[i];
                ssd.salary = Convert.ToDecimal(salary[i]);
                result2 = await Issd.Update(ssd);
            }
            int result = await Iss.Update(ss);
            if (result2 > 0)
            {
                return Content("<script>alert('复核成功');window.location.href='/salaryCriterion/salarystandard_check_list'</script>", "text/html", Encoding.Default);
            }
            else
            {
                return Content("<script>alert('复核失败');window.location.href='/salaryCriterion/salarystandard_check_list'</script>", "text/html", Encoding.Default);
            }




        }
    }
}
